package com.example.homecunter.video.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.homecunter.HomeCenterActivity;
import com.example.homecunter.R;
import com.example.homecunter.video.entity.ChannelEntity;
import com.example.homecunter.video.entity.VideoEntity;
import com.example.image.ImageLoader;
import com.example.image.ImageLoaderType;
import com.example.image.ImageOptions;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;


import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoViewHodel> {
    Context context;
    List<VideoEntity> videoData;

    LayoutInflater inflater;

    public VideoAdapter(Context context, List<VideoEntity> videoData) {
        this.context = context;
        this.videoData = videoData;

        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public VideoViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VideoViewHodel(inflater.inflate(R.layout.item_video,parent,false));
    }


    @Override
    public void onBindViewHolder(@NonNull VideoAdapter.VideoViewHodel holder, int position) {
        holder.gsyv.setUp(videoData.get(position).getVideopath(),false,"");

        holder.gsyv.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.gsyv.startWindowFullscreen(context,false,false);
            }
        });

        holder.gsyv.startPlayLogic();

//        Glide.with(context).load(videoData.get(position).getHeadpath()).transform(new RoundedCorners(50)).into(holder.img);

        ImageLoader imageLoader = new ImageLoader(ImageLoaderType.Glide);
        ImageOptions build = new ImageOptions.Builder().setPlaceImage(R.drawable.ic_launcher_background).build();
        imageLoader.loadFromUrl(context,videoData.get(position).getHeadpath(), holder.img, build);

        holder.item_tv.setText(videoData.get(position).getAuthname());


    }

    @Override
    public int getItemCount() {
        return videoData.size();
    }

    public class VideoViewHodel extends RecyclerView.ViewHolder {
        StandardGSYVideoPlayer gsyv;
        ImageView img;
        TextView item_tv;
        public VideoViewHodel(@NonNull View itemView) {
            super(itemView);
            gsyv = itemView.findViewById(R.id.item_video_player);
            img = itemView.findViewById(R.id.item_avatar_iv);
            item_tv = itemView.findViewById(R.id.item_text_tv);
        }
    }
}
